/****************************************************************************
**
** MenuGump.cpp
**
** Copyright (C) August 2014 Hotride
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
*****************************************************************************
*/
//----------------------------------------------------------------------------
#include "stdafx.h"
//---------------------------------------------------------------------------
void TMenuGump::GenerateFrame(TGump *gump, float PosX, float PosY)
{
	DWORD Index = (DWORD)gump;

	int PosXOffs = 0;
	int PageOffs = 0;
	
	if (gump->GetSelectionIndex())
	{
		if (gump->GetSelectionIndex() < 0) //Prev
		{
			PosXOffs = GUMP_MENU_PIXEL_STEP * gump->GetSelectionIndex();
			//gump->SetSelectionIndex(gump->GetSelectionIndex() + 1);
			PageOffs = 0;
		}
		else //Next
		{
			PosXOffs = -(gump->GetMiniX() - (gump->GetSelectionIndex() * GUMP_MENU_PIXEL_STEP));
			//gump->SetSelectionIndex(gump->GetSelectionIndex() - 1);
			PageOffs = -1;
		}
	}

	glNewList((GLuint)gump, GL_COMPILE);
	
		UO->DrawGump(0x0910, 0, PosX, PosY);

		UO->m_Font->DrawA(1, gump->GetName().c_str(), 0x0386, PosX + 39, PosY + 18, 200.0f, TS_FIXED);
		
		UO->m_GL.ViewPort(PosX + 40, PosY + 42, 217, 49);

		TGumpObject *go = gump->m_Items;
		int Idx = 1;
		string item_text = "";

		while (go != NULL)
		{
			if (Idx == gump->GetPage() + PageOffs)
			{
				float w = 0.0f;
				for (int i = 0; i < 7; i++)
				{
					if (go == NULL || w >= 254) break; //214 + 40 = 254 (40 - offset)
					POINT p = {0, 0};
					UO->GetArtDimension(go->GetGump() + 0x4000, p);
					if (p.x && p.y)
					{
						int OffsH = p.y;
						if (OffsH >= 47) OffsH = 0;
						else OffsH = ((47 - p.y) / 2);
						UO->DrawStaticArtInContainer(go->GetGump(), go->GetColor(), PosX + 40 + w + PosXOffs, PosY + 47 + OffsH);
						if (g_LastSelectedObject == Idx + 10) item_text = go->GetText();
						w += p.x;
					}

					Idx++;
					go = go->m_Next;
				}
				break;
			}

			Idx++;
			if (go != NULL) go = go->m_Next;
		}

		UO->m_GL.RestorePort();

		if (item_text.length()) UO->m_Font->DrawA(1, item_text.c_str(), 0x0386, PosX + 42, PosY + 105, 200.0f, TS_FIXED);

	glEndList();

	gump->SetID2(1);
	gump->SetListCreated(true);
}
//----------------------------------------------------------------------------
int TMenuGump::Draw(bool &mode, TGump *gump)
{
	DWORD Index = (DWORD)gump;

	int PosX = gump->GetX();
	int PosY = gump->GetY();

	bool NeedRedraw = false;
	
	if (gump->GetSelectionIndex())
	{
		NeedRedraw = true;
		if (gump->GetSelectionIndex() < 0) //Prev
		{
			gump->SetSelectionIndex(gump->GetSelectionIndex() + 1);
		}
		else //Next
		{
			gump->SetSelectionIndex(gump->GetSelectionIndex() - 1);
		}
	}

	if (g_LeftMouseDown && g_LastGumpLeftMouseDown == Index && (!g_LastObjectLeftMouseDown || g_LastObjectLeftMouseDown > 2))
	{
		PosX += (g_MouseX - g_DroppedLeftMouseX);
		PosY += (g_MouseY - g_DroppedLeftMouseY);

		if (mode) gump->SetID2(1);
	}

	if (mode)
	{
		if (!gump->GetSelectionIndex() && g_LastGumpLeftMouseDown == Index)
		{
			MoveItems(gump);
			GenerateFrame(gump, PosX, PosY);
		}
		else if ((Index == g_LastSelectedGump && g_LastSelectedObject >= 10) || NeedRedraw)
		{
			GenerateFrame(gump, PosX, PosY);
		}
		else if (gump->GetID2())
		{
			GenerateFrame(gump, PosX, PosY);
			gump->SetID2(0);
		}
	}


	if (mode)
	{
		if (!gump->GetListCreated()) GenerateFrame(gump, PosX, PosY);

		UO->DrawList((GLuint)gump, PosX, PosY);
	}
	else
	{
		if (UO->GumpPixelsInXY(0x0910, PosX, PosY))
		{
			g_LastSelectedObject = 0;
			g_LastSelectedGump = Index;
		}

		if (gump->GetSelectionIndex() || g_LastSelectedGump != Index) return 0;
		
		int LSG = 0;

		RECT rc = {0, 0, 8, 16};
		POINT p = {g_MouseX - (PosX + 23), g_MouseY - (PosY + 59)};
		if (PtInRect(&rc, p)) LSG = 1;

		p.x = g_MouseX - (PosX + 261);
		p.y = g_MouseY - (PosY + 59);
		if (PtInRect(&rc, p)) LSG = 2;
		
		RECT mrc = {0, 0, 217, 49};
		p.x = g_MouseX - (PosX + 40);
		p.y = g_MouseY - (PosY + 47);

		if (PtInRect(&mrc, p))
		{
			TGumpObject *go = gump->m_Items;
			int Idx = 1;
			while (go != NULL)
			{
				if (Idx == gump->GetPage())
				{
					float w = 0.0f;
					for (int i = 0; i < 7; i++)
					{
						if (go == NULL || w >= 214) break;
						POINT mp = {0, 0};
						UO->GetArtDimension(go->GetGump() + 0x4000, mp);
						if (mp.x && mp.y)
						{
							RECT irc = {0, 0, mp.x, 49};
							POINT ip = {g_MouseX - (PosX + 40 + w), g_MouseY - (PosY + 47)};
							if (PtInRect(&irc, ip))
							{
								LSG = Idx + 10;
								break;
							}
							w += mp.x;
						}
						Idx++;
						go = go->m_Next;
					}
				}
				if (LSG) break;
				Idx++;
				if (go != NULL) go = go->m_Next;
			}
		}

		if (LSG != 0) g_LastSelectedObject = LSG;

		return LSG;
	}
	return 0;
}
//----------------------------------------------------------------------------
void TMenuGump::OnButtonUp(TGump *gump)
{
	if (g_LastObjectLeftMouseDown != g_LastSelectedObject || !g_LastObjectLeftMouseDown || gump->GetMinimized() || !g_LastSelectedGump || gump->GetSelectionIndex()) return;

	MoveItems(gump);
}
//----------------------------------------------------------------------------
bool TMenuGump::OnLeftDoubleClick(TGump *gump)
{
	if (!g_LastSelectedGump || gump->GetSelectionIndex()) return false;

	if (g_LastObjectLeftMouseDown >= 10)
	{
		gump->SendMenuResponse(g_LastObjectLeftMouseDown - 10);
		return true;
	}

	return false;
}
//----------------------------------------------------------------------------
void TMenuGump::MoveItems(TGump *gump)
{
	if (g_LastObjectLeftMouseDown == 1) //Prev
	{
		TGumpObject *go = gump->m_Items;
		TGumpObject *prev = NULL;
		int Idx = 1;

		while (go != NULL)
		{
			if (Idx == gump->GetPage()) break;
			Idx++;
			prev = go;
			go = go->m_Next;
		}

		gump->SetMiniX(0);

		if (Idx == gump->GetPage() && go != NULL && go != gump->m_Items && prev != NULL)
		{
			POINT p = {0, 0};
			UO->GetArtDimension(prev->GetGump() + 0x4000, p);
			gump->SetMiniX(p.x);
			gump->AddSelectionIndex(-(p.x / GUMP_MENU_PIXEL_STEP));
		}

		gump->SetPage(gump->GetPage() - 1);
	}
	else if (g_LastObjectLeftMouseDown == 2) //Next
	{
		TGumpObject *go = gump->m_Items;
		int Idx = 1;

		while (go != NULL)
		{
			if (Idx == gump->GetPage()) break;
			Idx++;
			go = go->m_Next;
		}

		gump->SetMiniX(0);

		if (Idx == gump->GetPage() && go != NULL && go->m_Next != NULL)
		{
			POINT p = {0, 0};
			UO->GetArtDimension(go->GetGump() + 0x4000, p);
			gump->SetMiniX(p.x);
			gump->SetSelectionIndex(p.x / GUMP_MENU_PIXEL_STEP);
		}

		gump->IncPage();
	}

	if (gump->GetPage() < 1)
	{
		gump->SetMiniX(0);
		gump->SetSelectionIndex(0);
		gump->SetPage(1);
	}

	if (gump->GetPage() > gump->GetCount())
	{
		gump->SetMiniX(0);
		gump->SetSelectionIndex(0);
		gump->SetPage(gump->GetCount());
	}
}
//----------------------------------------------------------------------------